package org.itli.jnsocket.Service.ServiceImpl;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.itli.jnsocket.Mapper.GoodsMapper;
import org.itli.jnsocket.Mapper.NGoodsMapper;
import org.itli.jnsocket.Pojo.Goods;
import org.itli.jnsocket.Service.NGoodsService;
import org.itli.jnsocket.Util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;

@Slf4j
@Service
public class NGoodsServiceImpl implements NGoodsService {
    NGoodsMapper mapper;
    Gson gson;

    @Autowired
    public void setMapper(NGoodsMapper mapper) {
        this.mapper = mapper;
    }

    @Autowired
    public void setGson(Gson gson) {
        this.gson = gson;
    }

    @Override
    public String AllGoods(PageUtil page) {
        try{
            List<Goods> goods = mapper.AllGoods(page.getSize(), page.getPage());
            log.info("json:所有商品", goods.toString());
            String json = gson.toJson(goods);
            return json;
        }catch (Exception e){
            log.info("错误信息"+e.getMessage());
            return "error";
        }
    }

    @Override
    public String GoodsInWarehouse(BigInteger warehouse_id) {
        try{
            List<Goods> goods = mapper.AllGoodsInWarehouse(warehouse_id);
            String json = gson.toJson(goods);
            return json;
        } catch (Exception e) {
            log.info(e.getMessage());
            return "error";
        }
    }

    @Override
    public String GetGoods(String id) {
        try{
            Goods goods = mapper.GetGoods(id);
            System.out.println(goods);
            String json = gson.toJson(goods);
            return json;
        } catch (Exception e) {
            log.info("错误信息"+e.getMessage());
            return "error";
        }

    }

    @Override
    @Transactional//事物管理
    public String UpdateGoods(Goods goods, String id) {
        try{
            int wareHouseID = mapper.SelectWareHouseID(goods.getWarehouse_name());
            goods.setWarehouse_id(wareHouseID);
            int i = mapper.UpdateGoodsInfo(goods, id);
//            int i1 = mapper.UpdateGoodsStock(goods, id);
            if(i==1){
                return "1";
            }
            else {
                return "0";
            }
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    @Transactional
    public String AddGoods(Goods goods,String id) {
        try{
            int categoryID = mapper.SelectCategoryID(goods.getCategory_name());
            int i = mapper.InsertGoodsInfo(goods, id, categoryID);
//            int i2 = mapper.SelectGoodsId(goods.getGoods_sn());
//            int i1 = mapper.InsertGoodsStock(goods, id, i2);
            return "1";
        } catch (Exception e) {
            log.info(e.getMessage());
            return "error";
        }
    }


}
